package org.dsun.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Seter {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Method fetchSeter(Class objectClass,String fieldName){
		Class[] params=new Class[1];
		try {
			Field field=objectClass.getDeclaredField(fieldName);
			params[0]=field.getType();
			StringBuilder mn=new StringBuilder();
			mn.append("set");
			mn.append(fieldName.substring(0, 1).toUpperCase());
			mn.append(fieldName.substring(1));
			return objectClass.getMethod(mn.toString(), params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void invokeSeter(Object o,String fieldName,Object value){
		Method m=fetchSeter(o.getClass(), fieldName);
		try {
			m.invoke(o, new Object[]{value});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
